package com.lul15834.orderfood.activity.manager;

/**
 * 实现食物的信息
 */
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.lul15834.orderfood.R;
import com.lul15834.orderfood.bean.FoodBean;
import com.lul15834.orderfood.dao.FoodDao;
import com.lul15834.orderfood.until.FileImgUntil;

public class ManagerUpdateFood extends AppCompatActivity {
    private ActivityResultLauncher<String> getContentLauncher;
    Uri uri;
    String foodId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_manager_update_food);
        Toolbar toolbar=findViewById(R.id.man_updateFood_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ManagerUpdateFood.this,Manager.class);
                startActivity(intent);
            }
        });
        Intent intent=getIntent();
        FoodBean food;
        food=intent.getSerializableExtra("food", FoodBean.class);
        foodId=food.getFoodId();
        ImageView img=findViewById(R.id.man_updateFood_img);
        Bitmap bitmap= BitmapFactory.decodeFile(food.getFoodImage());
        img.setImageBitmap(bitmap);
        getContentLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {
                if (result != null) {
                    img.setImageURI(result);
                    uri=result;
                } else {
                    Toast.makeText(ManagerUpdateFood.this, "未选择图片", Toast.LENGTH_SHORT).show();
                }
            }
        });
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery(v);
            }
        });
        //将商品原由的图片设置成默认图片
        Drawable drawable=img.getDrawable();
        Bitmap  defaultDrawable =((BitmapDrawable) drawable).getBitmap();//获取图片的二进制文件
        EditText nameText=findViewById(R.id.man_updateFood_name);
        nameText.setText(food.getFoodName());
        EditText priceText=findViewById(R.id.man_updateFood_price);
        priceText.setText(food.getFoodPrice());
        EditText desText=findViewById(R.id.man_updateFood_des);
        desText.setText(food.getFoodDescription());
        Button addBtn=findViewById(R.id.man_updateFood_btn);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=nameText.getText().toString();
                String price=priceText.getText().toString();
                String des=desText.getText().toString();
                if (name.isEmpty()){
                    Toast.makeText(ManagerUpdateFood.this,"请输入商品名称",Toast.LENGTH_SHORT).show();
                }else if (price.isEmpty()){
                    Toast.makeText(ManagerUpdateFood.this,"请输入商品价格",Toast.LENGTH_SHORT).show();
                }else if (des.isEmpty()){
                    Toast.makeText(ManagerUpdateFood.this,"请输入商品描述",Toast.LENGTH_SHORT).show();
                }else {
                    Drawable drawable=img.getDrawable();
                    Bitmap bitmap =((BitmapDrawable) drawable).getBitmap();//获取图片的二进制文件
                    String path= FileImgUntil.getImgName();//图片存储路径名字
                    //检查Bitmap是否与默认鱼片相同
                    if (bitmap.sameAs(defaultDrawable)){//判断是不是默认图片
                        path=food.getFoodImage();

                    }else {
                        FileImgUntil.saveImageBitmapToFileImg(uri,ManagerUpdateFood.this,path);
                    }
                    FileImgUntil.saveImageBitmapToFileImg(uri,ManagerUpdateFood.this,path);//保存图片
                    //准备就绪，协议修改dao
                    int a=FoodDao.updateFood(food.getFoodId(), name,des,price,path);
                    if (a==1){
                        Toast.makeText(ManagerUpdateFood.this,"修改商品成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(ManagerUpdateFood.this,"修改商品失败",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
    private void openGallery(View v){
        getContentLauncher.launch("image/*");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.manage_food_del,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int a=item.getItemId();
        if (a==R.id.manage_food_del){
            //删除，点击删除，是否确认删除
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setTitle("删除商品");
            builder.setMessage("确定删除吗!");
            builder.setCancelable(false);
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int a=FoodDao.delFoodById(foodId);
                    if (a==1){
                        Toast.makeText(ManagerUpdateFood.this,"删除成功",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(ManagerUpdateFood.this,Manager.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(ManagerUpdateFood.this,"删除失败失败",Toast.LENGTH_SHORT).show();
                    }
                    dialog.dismiss();
                }
            });
            AlertDialog alertDialog=builder.create();
            alertDialog.show();
        }
        return super.onOptionsItemSelected(item);
    }
}